package com.xit.hitmouse.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ScoreDB {
		public static final String DB_NAME="score";
		public static final int VERSION = 3;
		private static ScoreDB scoreDB;
		private SQLiteDatabase db;
		
		private ScoreDB(Context context){
			DBHelper dbHlper = new DBHelper(context,DB_NAME,null,VERSION);
			
			db = dbHlper.getWritableDatabase();
		}
		
		public synchronized static ScoreDB getInstance(Context context){
			if(scoreDB==null){
				scoreDB = new ScoreDB(context);
			}
			return scoreDB;
		}
		
		public void saveScore(String name,int score){
			if(score!=0&&name!=null){
				ContentValues values = new ContentValues();
				values.put("author", name);
				values.put("score", score);
				db.insert("score", null, values);
			}
			
		}
		@SuppressLint("Range")
		public String[] serchScore(){
			String author = null;
			String score=null;
			Cursor cursor = db.rawQuery("select * from score where score=( select max(score) from  score)", null);
			while(cursor.moveToNext()){  
	             author = cursor.getString(cursor.getColumnIndex("author"));  
	             score = cursor.getInt(cursor.getColumnIndex("score"))+"";  
	        }  
			String [] arr=new String[2];
			arr[0]=author;
			arr[1]=score+"";
			return arr;
			
		}
}
